<template>
  <a-layout-header class="header">
    <div class="logo" />
    <a-menu
        theme="dark"
        mode="horizontal"
        v-model:selectedKeys="selectedKeys1"
        :style="{ lineHeight: '64px' }"
    >
      <a-menu-item key="/">
        <router-link to="/">首页</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/ebook" :style="user.id ? {}:{display:'none'}">
        <router-link to="/admin/ebook">电子书管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/user" :style="user.id ? {}:{display:'none'}">
        <router-link to="/admin/user">用户管理</router-link>
      </a-menu-item>
      <a-menu-item key="/admin/category" :style="user.id ? {}:{display:'none'}">
        <router-link to="/admin/category">分类管理</router-link>
      </a-menu-item>

      <a-menu-item key="/adout">
        <router-link to="/adout">关于我们</router-link>
      </a-menu-item>
      <a class="login-menu" v-show="user.id">
        <span>您好：{{user.name}}</span>
      </a>
      <a class="login-menu" v-show="!user.id" @click="showLoginModal">
        <span>登录</span>
      </a>
      <a-popconfirm
          title="确定是否退出登录?"
          ok-text="是"
          cancel-text="否"
          @confirm="logout"

      >
        <a class="login-menu" v-show="user.id" >
        <span>退出登录</span>
      </a>

      </a-popconfirm>
      </a-menu>
    <a-modal
        title="登录表单"
        v-model:visible="LoginmodalVisible"
        :confirm-loading="LoginmodalLoading"
        @ok="Login()"
    >
      <a-form   :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }"  :modal="loginUser">
        <a-form-item label="用户名" >
          <a-input v-model:value="loginUser.loginName" />
        </a-form-item>
        <a-form-item label="密码" >
          <a-input v-model:value="loginUser.password" type="password"/>
        </a-form-item>

      </a-form>
    </a-modal>
  </a-layout-header>

</template>
<script lang="ts">
import { defineComponent,ref ,computed} from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import store from "@/store";
declare let hexMd5: any;
declare let KEY: any;
export default defineComponent({
  name: 'the-header',
  setup(){
    const user = computed(() => store.state.user);
    const loginUser =ref({
      loginName:"test",
      password:"test123"
    });
    const LoginmodalVisible=ref(false);
    const LoginmodalLoading=ref(false);
    const showLoginModal = ( ) =>{
      LoginmodalVisible.value=true;
    }

    const Login = () =>{
      LoginmodalLoading.value=true;
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      axios.post("/user/login",loginUser.value).then((response) =>{
        const data=response.data
        LoginmodalVisible.value=false;
        if (data.success){
          store.commit("setUser", data.content);
          message.info("登录成功");
        }else {
          message.error(data.message);
        }
      });
    }
    const logout = () =>{

      axios.get("/user/logout/"+user.value.token).then((response) =>{
        const data=response.data
        LoginmodalVisible.value=false;
        if (data.success){
          store.commit("setUser", {});
          message.info("退出登录成功");
        }else {
          message.error(data.message);
        }
      });
    }
    return{
      loginUser,
      LoginmodalVisible,
      LoginmodalLoading,
      user,
      showLoginModal,
      Login,
      logout
    }
  }
});
</script>
<style>
.login-menu {
  float: right;
  color: white;
  padding-left: 10px;
}
</style>